
//% color="#3399FF" iconWidth=50 iconHeight=40
namespace djiMutipleTello{

    
    //% block="djitello begin " blockType="command"


    export function djitellopy_Iint(parameter: any, block: any) {

        
        Generator.addImport(`from djitellopy import Tello\nimport cv2\nfrom djitellopy import TelloSwarm`)
        Generator.addImport(`import requests\nfrom PIL import Image\nimport io\nimport subprocess\nimport re\nimport socket\nimport time\nimport os\nimport datetime\nimport random`)
    
    }

    //% block="djitello_mutiTT_ip " blockType="command"

    export function djitello_mutiTT_ip(parameter: any, block: any) {

        Generator.addCode(`Ip_list = []`)
        
    }

    //% block="djitello_mutiTT_ipappend [ip] [ip_number]" blockType="command"
    //% ip.shadow="string"  ip.defl="192.168.3.42"
    //% ip_number.shadow="ip_number"  ip_number.defl="0"

    export function djitello_mutiTT_ipappend(parameter: any, block: any) {
        let ip=parameter.ip.code;

        Generator.addCode(`Ip_list.append(${ip})`)
        
    }

    //% block="djitello_mutiTT_connect " blockType="command"

    export function djitello_mutiTT_connect(parameter: any, block: any) {

        Generator.addCode(`print(Ip_list)\nglobal swarm\nswarm= TelloSwarm.fromIps(Ip_list)\nswarm.connect()`)
        
    }

    //% block="djitello_mutiTT_search " blockType="command"

    export function djitello_mutiTT_search(parameter: any, block: any) {

        Generator.addCode(`socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)`)
        Generator.addCode(`socket.connect(('www.baidu.com', 80))\nlocal_ip = socket.getsockname()[0]\nlocal_ip_list=local_ip.split('.')`)
        Generator.addCode(`for i in range(2,255):\n    ip=f'{local_ip_list[0]}.{local_ip_list[1]}.{local_ip_list[2]}.{i}'\n    os.popen(f'ping -n 1 -w 500 {ip} > nul')`)
        Generator.addCode(`    time.sleep(0.01)`)
        Generator.addCode(`arp_table = subprocess.check_output(['arp', '-a'], universal_newlines=True)`)
        Generator.addCode(`pattern = rf'\\b({local_ip_list[0]}\\.{local_ip_list[1]}\\.{local_ip_list[2]}\\.\\d+)\\b'\nip_addresses = re.findall(pattern, arp_table)`)
        Generator.addCode(`ip_addresses.remove(local_ip)`)
        Generator.addCode(`if f'{local_ip_list[0]}.{local_ip_list[1]}.{local_ip_list[2]}.1' in ip_addresses:`)
        Generator.addCode(`    ip_addresses.remove(f'{local_ip_list[0]}.{local_ip_list[1]}.{local_ip_list[2]}.1')`)
        Generator.addCode(`if f'{local_ip_list[0]}.{local_ip_list[1]}.{local_ip_list[2]}.255' in ip_addresses:`)
        Generator.addCode(`    ip_addresses.remove(f'{local_ip_list[0]}.{local_ip_list[1]}.{local_ip_list[2]}.255')`)
        Generator.addCode(`print(ip_addresses)`)
        Generator.addCode(`snlist={}\niplist=[]`)
        Generator.addCode(`for ip in ip_addresses:\n    tello = Tello(ip,1)\n    try:\n        tello.connect(False)\n        snlist[tello.query_serial_number()]=ip\n        tello.end()`)
        Generator.addCode(`    except:\n        pass`)
        Generator.addCode(`print(f"搜索出来的sn码:{snlist}")`)

    }

    //% block="djitello_mutiTT_sn " blockType="command"

    export function djitello_mutiTT_sn(parameter: any, block: any) {

        Generator.addCode(`Ip_list = []`)
        
    }

    //% block="djitello_mutiTT_snappend [sn] [sn_number]" blockType="command"
    //% sn.shadow="string"  sn.defl="0TQZJ4DCNT0UD4"
    //% sn_number.shadow="ip_number"  sn_number.defl="0"

    export function djitello_mutiTT_snappend(parameter: any, block: any) {
        let sn=parameter.sn.code;

        Generator.addCode(`if snlist.get(${sn}):\n    Ip_list.append(snlist[${sn}])`)
        
    }

    //% block="djitello_mutiTT_motoron " blockType="command"

    export function djitello_mutiTT_motoron(parameter: any, block: any) {

        Generator.addCode(`swarm.turn_motor_on()`)
        
    }
    
    //% block="djitello_mutiTT_motoroff " blockType="command"

    export function djitello_mutiTT_motoroff(parameter: any, block: any) {

        Generator.addCode(`swarm.turn_motor_off()`)
        
    }

    //% block="djitello_mutiTT_takeoff " blockType="command"

    export function djitello_mutiTT_takeoff(parameter: any, block: any) {

        Generator.addCode(`swarm.takeoff()`)
        
    }

    //% block="djitello_mutiTT_land " blockType="command"

    export function djitello_mutiTT_land(parameter: any, block: any) {

        Generator.addCode(`swarm.land()`)
        
    }

    //% block="djitello_mutiTT_aloneLed [LEDCOLOR] [LED_number]" blockType="command"
    //% LED_number.shadow="number"  LED_number.defl="0"
    //% LEDCOLOR.shadow="dropdown" LEDCOLOR.options="LEDCOLOR"
    
    export function djitello_mutiTT_aloneLed(parameter: any, block: any) {
        let ledcolor=parameter.LEDCOLOR.code;
        let led_number=parameter.LED_number.code;
 
        Generator.addCode(`swarm.sequential(lambda i, tello: tello.send_expansion_command("${ledcolor}") if i == ${led_number} else None)`)

    }

    //% block="djitello_mutiTT_aloneRGBLed [R] [G] [B] [RGBLED_number]" blockType="command"
    //% RGBLED_number.shadow="number"  RGBLED_number.defl="0"
    //% R.shadow="number"  R.defl="255"
    //% G.shadow="number"  G.defl="255"
    //% B.shadow="number"  B.defl="255"
    
    export function djitello_mutiTT_aloneRGBLed(parameter: any, block: any) {
        let RGBLED_number=parameter.RGBLED_number.code;
        let R=parameter.R.code;
        let G=parameter.G.code;
        let B=parameter.B.code;
 
        Generator.addCode(`swarm.sequential(lambda i, tello: tello.send_expansion_command("led ${R} ${G} ${B}") if i == ${RGBLED_number} else None)`)

    }

    //% block="djitello_mutiTT_aloneBreath [t] [Rb] [Gb] [Bb] [Breath_number]" blockType="command"
    //% Breath_number.shadow="number"  Breath_number.defl="0"
    //% t.shadow="number"  t.defl="0.1"
    //% Rb.shadow="number"  Rb.defl="255"
    //% Gb.shadow="number"  Gb.defl="255"
    //% Bb.shadow="number"  Bb.defl="255"

    export function djitello_mutiTT_aloneBreath(parameter: any, block: any) {
        let Breath_number=parameter.Breath_number.code;
        let t=parameter.t.code;
        let Rb=parameter.Rb.code;
        let Gb=parameter.Gb.code;
        let Bb=parameter.Bb.code;

        Generator.addCode(`swarm.sequential(lambda i, tello: tello.send_expansion_command("led br ${t} ${Rb} ${Gb} ${Bb}") if i == ${Breath_number} else None)`)

    }

    //% block="djitello_mutiTT_aloneFilcker [t_flicker] [R1] [G1] [B1] [R2] [G2] [B2] [Filcker_number]" blockType="command"
    //% Filcker_number.shadow="number"  Filcker_number.defl="0"
    //% t_flicker.shadow="number"  t_flicker.defl="0.1"
    //% R1.shadow="number"  R1.defl="255"
    //% G1.shadow="number"  G1.defl="255"
    //% B1.shadow="number"  B1.defl="255"
    //% R2.shadow="number"  R2.defl="255"
    //% G2.shadow="number"  G2.defl="255"
    //% B2.shadow="number"  B2.defl="255"

    export function djitello_mutiTT_aloneFilcker(parameter: any, block: any) {
        let Filcker_number=parameter.Filcker_number.code;
        let t_flicker=parameter.t_flicker.code;
        let R1=parameter.R1.code;
        let G1=parameter.G1.code;
        let B1=parameter.B1.code;
        let R2=parameter.R2.code;
        let G2=parameter.G2.code;
        let B2=parameter.B2.code;
 
        Generator.addCode(`swarm.sequential(lambda i, tello: tello.send_expansion_command("led bl ${t_flicker} ${R1} ${G1} ${B1} ${R2} ${G2} ${B2}") if i == ${Filcker_number} else None)`)

    }

    //% block="djitello_mutiTT_aloneMLed [MLEDCOLOR] [MLEDSTRING] [MLED_number]" blockType="command"
    //% MLED_number.shadow="number"  MLED_number.defl="0"
    //% MLEDCOLOR.shadow="dropdown" MLEDCOLOR.options="MLEDCOLOR"
    //% MLEDSTRING.shadow="dropdown" MLEDSTRING.options="MLEDSTRING"
    
    export function djitello_mutiTT_aloneMLed(parameter: any, block: any) {
        let mledcolor=parameter.MLEDCOLOR.code;
        let mledstring=parameter.MLEDSTRING.code;
        let mled_number=parameter.MLED_number.code;
 
        Generator.addCode(`swarm.sequential(lambda i, tello: tello.send_expansion_command("mled s ${mledcolor} ${mledstring}") if i == ${mled_number} else None)`)

    }

    //% block="djitello_mutiTT_alonemotoron [motoron_number]" blockType="command"
    //% motoron_number.shadow="number"  motoron_number.defl="0"
    
    export function djitello_mutiTT_alonemotoron(parameter: any, block: any) {
        let motoron_number=parameter.motoron_number.code;

        Generator.addCode(`swarm.sequential(lambda i, tello: tello.turn_motor_on() if i == ${motoron_number} else None)`)
        
    }

    //% block="djitello_mutiTT_alonemotoroff [motoroff_number]" blockType="command"
    //% motoroff_number.shadow="number"  motoroff_number.defl="0"
    
    export function djitello_mutiTT_alonemotoroff(parameter: any, block: any) {
        let motoroff_number=parameter.motoroff_number.code;

        Generator.addCode(`swarm.sequential(lambda i, tello: tello.turn_motor_off() if i == ${motoroff_number} else None)`)
        
    }

    //% block="djitello_mutiTT_alonetakeoff [takeoff_number]" blockType="command"
    //% takeoff_number.shadow="number"  takeoff_number.defl="0"
    
    export function djitello_mutiTT_alonetakeoff(parameter: any, block: any) {
        let takeoff_number=parameter.takeoff_number.code;

        Generator.addCode(`swarm.sequential(lambda i, tello: tello.takeoff() if i == ${takeoff_number} else None)`)
        
    }

    //% block="djitello_mutiTT_aloneland [land_number]" blockType="command"
    //% land_number.shadow="number"  land_number.defl="0"
    
    export function djitello_mutiTT_aloneland(parameter: any, block: any) {
        let land_number=parameter.land_number.code;

        Generator.addCode(`swarm.sequential(lambda i, tello: tello.land() if i == ${land_number} else None)`)
        
    }

    //% block="djitello_mutiTT_aloneup [up_distance] [up_number] " blockType="command"
    //% up_number.shadow="number"  up_number.defl="0"
    //% up_distance.shadow="number"  up_distance.defl="20"

    export function djitello_mutiTT_aloneup(parameter: any, block: any) {
        let up_distance=parameter.up_distance.code;
        let up_number=parameter.up_number.code;

        Generator.addCode(`swarm.sequential(lambda i, tello: tello.move_up(${up_distance}) if i == ${up_number} else None)`)
        
    }

    //% block="djitello_mutiTT_alonedown [down_distance] [down_number] " blockType="command"
    //% down_number.shadow="number"  down_number.defl="0"
    //% down_distance.shadow="number"  down_distance.defl="20"

    export function djitello_mutiTT_alonedown(parameter: any, block: any) {
        let down_distance=parameter.down_distance.code;
        let down_number=parameter.down_number.code;

        Generator.addCode(`swarm.sequential(lambda i, tello: tello.move_down(${down_distance}) if i == ${down_number} else None)`)
        
    }

    //% block="djitello_mutiTT_aloneforward [forward_distance] [control_number] " blockType="command"
    //% control_number.shadow="number"  control_number.defl="0"
    //% forward_distance.shadow="number"  forward_distance.defl="20"

    export function djitello_mutiTT_aloneforward(parameter: any, block: any) {
        let forward_distance=parameter.forward_distance.code;
        let control_number=parameter.control_number.code;

        Generator.addCode(`swarm.sequential(lambda i, tello: tello.move_forward(${forward_distance}) if i == ${control_number} else None)`)
        
    }

    //% block="djitello_mutiTT_aloneback [back_distance] [back_number] " blockType="command"
    //% back_number.shadow="number"  back_number.defl="0"
    //% back_distance.shadow="number"  back_distance.defl="20"

    export function djitello_mutiTT_aloneback(parameter: any, block: any) {
        let back_distance=parameter.back_distance.code;
        let back_number=parameter.back_number.code;

        Generator.addCode(`swarm.sequential(lambda i, tello: tello.move_back(${back_distance}) if i == ${back_number} else None)`)
        
    }

    //% block="djitello_mutiTT_aloneleft [left_distance] [left_number] " blockType="command"
    //% left_number.shadow="number"  left_number.defl="0"
    //% left_distance.shadow="number"  left_distance.defl="20"

    export function djitello_mutiTT_aloneleft(parameter: any, block: any) {
        let left_distance=parameter.left_distance.code;
        let left_number=parameter.left_number.code;

        Generator.addCode(`swarm.sequential(lambda i, tello: tello.move_left(${left_distance}) if i == ${left_number} else None)`)
        
    }

    //% block="djitello_mutiTT_aloneright [right_distance] [right_number] " blockType="command"
    //% right_number.shadow="number"  right_number.defl="0"
    //% right_distance.shadow="number"  right_distance.defl="20"

    export function djitello_mutiTT_aloneright(parameter: any, block: any) {
        let right_distance=parameter.right_distance.code;
        let right_number=parameter.right_number.code;

        Generator.addCode(`swarm.sequential(lambda i, tello: tello.move_right(${right_distance}) if i == ${right_number} else None)`)
        
    }

    //% block="djitello_mutiTT_alonerotate [Rotate_Pattern] [rotate_degrees] [rotate_number] " blockType="command"
    //% Rotate_Pattern.shadow="dropdown"  Rotate_Pattern.options="Rotate_Pattern"
    //% rotate_number.shadow="number"  rotate_number.defl="0"
    //% rotate_degrees.shadow="number"  rotate_degrees.defl="5"

    export function djitello_mutiTT_alonerotate(parameter: any, block: any) {
        let Rotate_Pattern=parameter.Rotate_Pattern.code;
        let rotate_degrees=parameter.rotate_degrees.code;
        let rotate_number=parameter.rotate_number.code;

        Generator.addCode(`swarm.sequential(lambda i, tello: tello.${Rotate_Pattern}(${rotate_degrees})  if i == ${rotate_number} else None)`)
        
    }

    //% block="djitello_mutiTT_aloneRelative [relative_x] [relative_y] [relative_z] [relative_speed] [relative_number] " blockType="command"
    //% relative_x.shadow="number"  relative_x.defl="0"
    //% relative_y.shadow="number"  relative_y.defl="0"
    //% relative_z.shadow="number"  relative_z.defl="0"
    //% relative_speed.shadow="number"  relative_speed.defl="10"
    //% relative_number.shadow="number"  relative_number.defl="0"

    export function djitello_mutiTT_aloneRelative(parameter: any, block: any) {
        let relative_x=parameter.relative_x.code;
        let relative_y=parameter.relative_y.code;
        let relative_z=parameter.relative_z.code;
        let relative_speed=parameter.relative_speed.code;
        let relative_number=parameter.relative_number.code;

        Generator.addCode(`swarm.sequential(lambda i, tello: tello.go_xyz_speed(${relative_x}, ${relative_y}, ${relative_z}, ${relative_speed})  if i == ${relative_number} else None)`)
        
    }

    //% block="djitello_mutiTT_alonechallengeid [id_number] [mission_id]" blockType="command"
    //% id_number.shadow="number"  id_number.defl="0"
    //% mission_id.shadow="number"

    export function djitello_mutiTT_alonechallengeid(parameter: any, block: any) {
        let id_number=parameter.id_number.code;
        let mission_id=parameter.mission_id.code;

        Generator.addCode(`def id_execute(i, tello):\n    if i == ${id_number}:`)
        Generator.addCode(`        global ${mission_id}\n        ${mission_id} = tello.get_mission_pad_id()\n        print(${mission_id})`)
        Generator.addCode(`swarm.sequential(id_execute)`)

    }

    //% block="djitello_mutiTT_alonechallengeXYZ [XYZ_number] [XYZ_choice] [XYZ_distance]" blockType="command"
    //% XYZ_number.shadow="number"  XYZ_number.defl="0"
    //% XYZ_choice.shadow="dropdown"  XYZ_choice.options="XYZ_choice"
    //% XYZ_distance.shadow="number"  XYZ_distance.defl="0"

    export function djitello_mutiTT_alonechallengeXYZ(parameter: any, block: any) {
        let XYZ_number=parameter.XYZ_number.code;
        let XYZ_choice=parameter.XYZ_choice.code;
        let XYZ_distance=parameter.XYZ_distance.code;

        Generator.addCode(`def XYZ_execute(i, tello):\n    if i == ${XYZ_number}:`)
        Generator.addCode(`        global ${XYZ_distance}\n        ${XYZ_distance} = tello.${XYZ_choice}\n        print(${XYZ_distance})`)
        Generator.addCode(`swarm.sequential(XYZ_execute)`)
        
    }

    //% block="djitello_mutiTT_Allchallenge " blockType="command"

    export function djitello_mutiTT_Allchallenge(parameter: any, block: any) {

        Generator.addCode(`swarm.enable_mission_pads()`)
        
    }

    //% block="djitello_mutiTT_Allchallenge1 " blockType="command"

    export function djitello_mutiTT_Allchallenge1(parameter: any, block: any) {

        Generator.addCode(`swarm.set_mission_pad_detection_direction(0)`)
        
    }


    //% block="djitello_mutiTT_alonechallenge [control_number1]" blockType="command"
    //% control_number1.shadow="number"  control_number1.defl="0"

    export function djitello_mutiTT_alonechallenge(parameter: any, block: any) {
        let control_number1=parameter.control_number1.code;

        Generator.addCode(`swarm.sequential(lambda i, tello: tello.enable_mission_pads() if i == ${control_number1} else None)`)
        
    }

    //% block="djitello_mutiTT_alonechallenge1 [control_number2]" blockType="command"
    //% control_number2.shadow="number"  control_number2.defl="0"

    export function djitello_mutiTT_alonechallenge1(parameter: any, block: any) {
        let control_number2=parameter.control_number2.code;

        Generator.addCode(`swarm.sequential(lambda i, tello: tello.set_mission_pad_detection_direction(0) if i == ${control_number2} else None)`)
        
    }

    //% block="djitello_mutiTT_alonechallengefly [mutix] [mutiy] [mutiz] [mutispeed] [mutimid] [control_number3]" blockType="command"
    //% mutix.shadow="number"  mutix.defl="0"
    //% mutiy.shadow="number"  mutiy.defl="0"
    //% mutiz.shadow="number"  mutiz.defl="0"
    //% mutispeed.shadow="number"  mutispeed.defl="10"
    //% mutimid.shadow="number"  mutimid.defl="1"
    //% control_number3.shadow="number"  control_number3.defl="0"

    export function djitello_mutiTT_alonechallengefly(parameter: any, block: any) {
        let mutix=parameter.mutix.code;
        let mutiy=parameter.mutiy.code;
        let mutiz=parameter.mutiz.code;
        let mutispeed=parameter.mutispeed.code;
        let mutimid=parameter.mutimid.code;
        let control_number3=parameter.control_number3.code;

        Generator.addCode(`try:\n    swarm.sequential(lambda i, tello: tello.go_xyz_speed_mid(${mutix}, ${mutiy}, ${mutiz}, ${mutispeed}, ${mutimid}) if i == ${control_number3} else None)\nexcept:\n    pass`)        
    }

    //% block="djitello_mutiTT_alonestreamon [streamon_number]" blockType="command"
    //% streamon_number.shadow="number"  streamon_number.defl="0"

    export function djitello_mutiTT_alonestreamon(parameter: any, block: any) {
        let streamon_number=parameter.streamon_number.code;

        Generator.addCode(`swarm.sequential(lambda i, tello: tello.streamon() if i == ${streamon_number} else None)`)
        Generator.addCode(`swarm.sequential(lambda i, tello: tello.set_video_fps('high') if i == ${streamon_number} else None)`)
        Generator.addCode(`swarm.sequential(lambda i, tello: tello.set_video_bitrate(5) if i == ${streamon_number} else None)`)
        
    }

    //% block="djitello_mutiTT_alonestreamoff [streamoff_number]" blockType="command"
    //% streamoff_number.shadow="number"  streamoff_number.defl="0"

    export function djitello_mutiTT_alonestreamoff(parameter: any, block: any) {
        let streamoff_number=parameter.streamoff_number.code;

        Generator.addCode(`swarm.sequential(lambda i, tello: tello.streamoff() if i == ${streamoff_number} else None)`)
        
    }

    //% block="djitello_mutiTT_streamupload [upload_number] [upload_url]" blockType="command"
    //% upload_number.shadow="number"  upload_number.defl="0"
    //% upload_url.shadow="string"  upload_url.defl="http://localhost:5000/"

    export function djitello_mutiTT_streamupload(parameter: any, block: any) {
        let upload_number=parameter.upload_number.code;
        let upload_url=parameter.upload_url.code;

        Generator.addCode(`def execute_commands(i, tello):\n    if i == ${upload_number}:\n        image = Image.fromarray(tello.get_frame_read().frame.astype('uint8'))`)
        Generator.addCode(`        image_bytes = io.BytesIO()\n        image.save(image_bytes, format='png')\n        filename = f"{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}{random.randint(0, 99):02d}.jpg"\n        files = {'file': (filename, image_bytes.getvalue())}`)
        Generator.addCode(`        response = requests.post(${upload_url}, files=files)\n        print(response)\n        print(response.text)`)
        Generator.addCode(`swarm.sequential(execute_commands)`)

    }

    //% block="djitello_mutiTT_end " blockType="command"

    export function djitello_mutiTT_end(parameter: any, block: any) {

        Generator.addCode(`swarm.end()`)
        
    }

}
